data structures dp implementation two pointers *2000

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int inf = 1e9+7;

int t = 1;

void solve() {
	int n;
	cin >> n;
	
	int ans = 0;
	vector<int> a(n);
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	
	for (int v = 1; v <= 30; ++v) {
		int i = 0, s = 0;
		
		while (i < n) {
			for (; i < n && a[i] > v; ++i) { ; }
			
			for (; i < n && a[i] <= v; ++i) {
				s = max(s + a[i], a[i]);
				ans = max(ans, s - v);
			}	
		}
		
	}
	
	cout << ans << '\n';
}

int main() {
	cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(false);
	// cin >> t;
	while (t--) {
		solve();	
	}
	return 0;
}


Comments

Submit
0 Comments
More Questions

Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses